using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using QuoteTree;
using qman;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;



public partial class print_quote : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		QTree tree;
		if (!IsPostBack)
		{
			if (this.Request.QueryString["quote"] != null)
			{
				BinaryFormatter formater = new BinaryFormatter();
				byte[] bytes_stream;
				DataSet ds2 = qman.DataHelp.DataAccess.getDataSet("select quote,cmp_id from adtech_quotes where id=" + this.Request.QueryString["quote"].ToString());
				bytes_stream = ds2.Tables[0].Rows[0][0] as byte[];
				ID = ds2.Tables[0].Rows[0][1].ToString();
				MemoryStream memory_stream = new MemoryStream(bytes_stream);
				tree = (formater.Deserialize(memory_stream)) as QTree;
				tree.Root.selected = true;
				this.Session["tree"] = tree;

			}
			if (this.Session["tree"] != null)
			{

				string[] list = (this.Session["tree"] as QTree).GetSelectionsString((this.Session["tree"] as QTree).Root).Split("|".ToCharArray());
				foreach (string s in list)
				{
					string s1 = s.Trim();
					if (s1 != "")
					{
                        this.TD1.InnerHtml += "<font face=\"Courier New\" size=2>" + s1.Replace("\n", "").Replace("\r", "") + "</font>" + "<BR>";// +"<HR size=1>";

					}
				}

				if (Request.QueryString["cmp_id"] != null && Request.QueryString["cmp_id"].ToString() != "")
				{
					string ID = Request.QueryString["cmp_id"].ToString().Replace("'", "~");
					DataSet ds = qman.DataHelp.DataAccess.getDataSet("select name, address, address2, city, state, zip, phone from companies where id = '" + ID + "'");

					//If the company exists in the database....then..
					if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
					{
						//Set company name, address, city, etc...
						this.Label3.Text = ds.Tables[0].Rows[0][0].ToString();
						this.Label4.Text = ds.Tables[0].Rows[0][1].ToString();
						this.Label7.Text = ds.Tables[0].Rows[0][2].ToString();
						this.Label8.Text = ds.Tables[0].Rows[0][3].ToString() + ", " + ds.Tables[0].Rows[0][4].ToString() + ", " + ds.Tables[0].Rows[0][5].ToString();
						this.Label2.Text = "$" + decimal.Round((this.Session["tree"] as QTree).Root.total(), 0).ToString();

						//If it is a revision get values previously saved in the database.
						if (this.Request.QueryString["quote"] != null && this.Request.QueryString["quote"].ToString() != "")
						{
							string quote = this.Request.QueryString["quote"].ToString().Replace("'", "~");
							DataSet ds1 = qman.DataHelp.DataAccess.getDataSet("select id, date, created_by from adtech_quotes where id=" + quote + " and " + "cmp_id='" + ID + "'");
							if (ds1 != null && ds1.Tables.Count > 0 && ds1.Tables[0].Rows.Count > 0)
							{
								this.Label1.Text = ds1.Tables[0].Rows[0][2].ToString();
								this.Label5.Text = ds1.Tables[0].Rows[0][1].ToString();
								this.Label6.Text = ds1.Tables[0].Rows[0][0].ToString();
							}
							else Response.Redirect("success.aspx?message=The company and the quote number dont match. Please contact webmaster.");

						}

						else
						{
							this.Label1.Text = this.Session["User"].ToString();
							this.Label5.Text = DateTime.Today.ToShortDateString();
						}
					}

				}

				this.HyperLink1.Attributes.Add("onclick", "javascript:document.all(\"HyperLink1\").style.display = \"none\";window.print();");
			}

		}

	}
}
